package com.charlie.sessions;

import com.charlie.beans.Users;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Вова
 */
@Stateless
public class UsersFacade extends AbstractFacade<Users> implements UsersFacadeLocal {
    @PersistenceContext(unitName = "CharlieShop-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UsersFacade() {
        super(Users.class);
    }

    @Override
    public Users findByEmail(String email) {
        try {
            return (Users) em.createNamedQuery("Users.findByEmail").setParameter("email", email).getSingleResult();
        } catch(NoResultException ex) {
            return null;
        }
    }

    @Override
    public Users findByEmailAndPassword(String email, String password) {
        try {
            Query query = em.createNamedQuery("Users.findByEmailAndPassword");
            query.setParameter("email", email);
            query.setParameter("password", password);
            return (Users) query.getSingleResult();
        } catch(NoResultException ex) {
            return null;
        }
    }

    @Override
    public List<Users> findByFirstName(String firstName) {
        return (List<Users>) em.createNamedQuery("Users.findByFirstName").setParameter("firstName", firstName).getResultList();
    }

    @Override
    public List<Users> findBySecondName(String secondName) {
        return (List<Users>) em.createNamedQuery("Users.findBySecondName").setParameter("secondName", secondName).getResultList();
    }

    @Override
    public List<Users> findByLocation(String location) {
        return (List<Users>) em.createNamedQuery("Users.findByLocation").setParameter("location", location).getResultList();
    }

    @Override
    public List<Users> findByCreationDate(String creationDate) {
        return (List<Users>) em.createNamedQuery("Users.findByCreationDate").setParameter("creationDate", creationDate).getResultList();
    }

    @Override
    public List<Users> findByStatus(String status) {
        return (List<Users>) em.createNamedQuery("Users.findByStatus").setParameter("status", status).getResultList();
    }

    @Override
    public boolean emailIsValid(String email) {
        if (email != null) {
            return email.matches("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
        }
        return false;
    }

    @Override
    public Users findByUsersId(Integer usersId) {
        try {
            return (Users) em.createNamedQuery("Users.findByUsersId").setParameter("usersId", usersId).getSingleResult();
        } catch(NoResultException ex) {
            return null;
        }
    }

    @Override
    public boolean nameIsValid(String name) {
        if (name != null) {
            return name.matches("(?i)[a-z0-9 ']{1,32}");
        }
        return false;
    }
}
